//
//  ReWriteTabBar.m
//  07-HomeWork
//
//  Created by dllo on 16/7/23.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ReWriteTabBar.h"
#import "FirstPageViewController.h"
#import "NewsViewController.h"
#import "FindViewController.h"
#import "MyViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ReWriteTabBar ()
@property (nonatomic, strong) UIView *view_Back;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UITabBarController *hidTabBar;
@end

@implementation ReWriteTabBar
- (void)viewWillAppear:(BOOL)animated {
    FirstPageViewController *first = [[FirstPageViewController alloc] init];
    [self presentViewController:first animated:YES completion:nil];
    [first.view addSubview:_view_Back];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createBackView];
    [self createButton];
    [self assignment];
    [self createTabBar];
    
}

- (void)createTabBar {
    FirstPageViewController *first = [[FirstPageViewController alloc] init];
    NewsViewController *news = [[NewsViewController alloc] init];
    FindViewController *find = [[FindViewController alloc] init];
    MyViewController *my = [[MyViewController alloc] init];
    self.hidTabBar = [[UITabBarController alloc] init];
    self.hidTabBar.viewControllers = @[first, find, news, my];
    self.hidTabBar.tabBar.userInteractionEnabled = YES;
    
}
- (void) createBackView {
    self.view_Back = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 49, WIDTH, 49)];
    _view_Back.backgroundColor =  [UIColor colorWithRed:0.859 green:0.831 blue:0.831 alpha:1.000];
    [self.hidTabBar.tabBar addSubview:_view_Back];
    self.view_Back.userInteractionEnabled = YES;
}
- (void)createButton {
    for (int i = 0; i < 4; i++) {
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.frame = CGRectMake(40 + i * (WIDTH - 80 - 30) / 3 , 2, 30, 30);
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(40 + i * (WIDTH - 80 - 30) / 3, 32, 30, 15)];
        _button.userInteractionEnabled = YES;
        [_view_Back addSubview:_button];
        [_view_Back addSubview:_label];
        
        _button.tag = 100 + i;
        _label.tag = 200 + i;
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i]];
        [_button setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        if (_label.tag == 200) {
            _label.text = @"首页";
            _label.textAlignment = NSTextAlignmentCenter;
            _label.font = [UIFont systemFontOfSize:13];
        }
        if (_label.tag == 201) {
            _label.text = @"消息";
            _label.textAlignment = NSTextAlignmentCenter;
            _label.font = [UIFont systemFontOfSize:13];
        }
        if (_label.tag == 202) {
            _label.text = @"发现";
            _label.textAlignment = NSTextAlignmentCenter;
            _label.font = [UIFont systemFontOfSize:13];
        }
        if (_label.tag == 203) {
            _label.text = @"我";
            _label.textAlignment = NSTextAlignmentCenter;
            _label.font = [UIFont systemFontOfSize:13];
        }
    }
}
- (void)assignment {
    UIButton *firstPageButton = [self.view viewWithTag:100];
    [firstPageButton addTarget:self action:@selector(handleNews:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *newsButton = [self.view viewWithTag:101];
    [newsButton addTarget:self action:@selector(handleNews:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *findButton = [self.view viewWithTag:102];
    [findButton addTarget:self action:@selector(handleNews:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *myButton = [self.view viewWithTag:103];
    [myButton addTarget:self action:@selector(handleNews:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)handleNews:(UIButton *)button {
    [self.tabBar addSubview:_view_Back];
    if ([button isEqual:[self.view viewWithTag:100]]) {
        self.hidTabBar.viewControllers[0].modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:self.hidTabBar.viewControllers[0] animated:YES completion:nil];
        
    } else if ([button isEqual:[self.view viewWithTag:101]]) {
        self.hidTabBar.viewControllers[1].modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:self.hidTabBar.viewControllers[1] animated:YES completion:nil];
    } else if ([button isEqual:[self.view viewWithTag:102]]) {
        self.hidTabBar.viewControllers[2].modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:self.hidTabBar.viewControllers[2] animated:YES completion:nil];
    } else if([button isEqual:[self.view viewWithTag:103]]) {
        self.hidTabBar.viewControllers[3].modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:self.hidTabBar.viewControllers[3] animated:YES completion:nil];
    }
    
    NewsViewController *news = [[NewsViewController alloc] init];
    [self presentViewController:news animated:YES completion:nil];
    [news.view addSubview:_view_Back];
}

//- (void)handleFirst:(UIButton *)button {
//    if ([button isEqual:[self.view viewWithTag:100]]) {
//        
//    }
//    FirstPageViewController *first = [[FirstPageViewController alloc] init];
//    [self presentViewController:self.tabBarController.viewControllers[0] animated:YES completion:nil];
//    [first.view addSubview:_view_Back];
//}
//- (void)handleNews:(UIButton *)button {
//    NewsViewController *news = [[NewsViewController alloc] init];
//    [self presentViewController:news animated:YES completion:nil];
//    [news.view addSubview:_view_Back];
//}
//- (void)handleFind:(UIButton *)button {
//    FindViewController *find = [[FindViewController alloc] init];
//    [self presentViewController:find animated:YES completion:nil];
//    [find.view addSubview:_view_Back];
//}
//- (void)handleMy:(UIButton *)button {
//    MyViewController *my = [[MyViewController alloc] init];
//    [self presentViewController:my animated:YES completion:nil];
//    [my.view addSubview:_view_Back];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
